package com.example.xclient

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.fdbus.IPCNameManager
import com.example.xclient.bean.INameServer
import com.example.xclient.ui.theme.NameServerDemoTheme

class MainActivity : ComponentActivity() {
    private lateinit var iNameServer: INameServer
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NameServerDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
                ) {
                    MainLayout()
                }
            }
        }
        IPCNameManager.init(this)
    }

    enum class ClickType {
        TYPE_CONNECT, TYPE_SERVER_FOUND, TYPE_SERVER_INVOKE
    }


    private fun onClickListener(id: ClickType) {
        when (id) {
            ClickType.TYPE_CONNECT -> {
                IPCNameManager.connect("ws://127.0.0.1")
            }

            ClickType.TYPE_SERVER_FOUND -> {
                iNameServer = IPCNameManager.getInstance(INameServer::class.java, 123, "123")
                Log.i("XClient", "onClickListener: ")
            }

            ClickType.TYPE_SERVER_INVOKE -> {
                val speed = iNameServer.getSpeed("123", "234")
                Log.i("XClient", "onClickListener: ")
            }
        }
    }

    @Composable
    fun MainLayout() {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            val modifier = Modifier.fillMaxWidth()
            Button(modifier = modifier, onClick = { onClickListener(ClickType.TYPE_CONNECT) }) {
                Text(text = "连接")
            }
            Button(
                modifier = modifier,
                onClick = { onClickListener(ClickType.TYPE_SERVER_FOUND) }) {
                Text(text = "服务发现")
            }
            Button(
                modifier = modifier,
                onClick = { onClickListener(ClickType.TYPE_SERVER_INVOKE) }) {
                Text(text = "服务调用")
            }
        }
    }

    @Preview(showBackground = true)
    @Composable
    fun MainLayoutPreview() {
        NameServerDemoTheme {
            MainLayout()
        }
    }
}



